<?php

class Default_ErrorController extends Ac_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        //for test
        if (/*defined('API_TEST')*/true) {
            echo json_encode('File: ' . $errors->exception->getFile() . ', Line: '.$errors->exception->getLine() . ', '.$errors->exception->getMessage());
            exit;
        }

        switch ($errors->type) {
            //case Ac_Exception_MissingListing:
             //   break;

            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
        
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:

                    $this->getResponse()->setHttpResponseCode(404);
                    if ($errors->exception instanceof Ac_Exception_MissingListing) {
                        $this->view->obj = $this->view->t('Ad');
                    } elseif ($errors->exception instanceof Ac_Exception_MissingCategory) {
                        $this->view->obj = $this->view->t('Category');
                    } elseif ($errors->exception instanceof Ac_Exception_MissingUser) {
                        $this->view->obj = $this->view->t('User');
                    } else {
                        $this->view->obj = $this->view->t('Page');
                    }

                    if (!DISPLAY_DEBUG) {
                        file_put_contents('/tmp/errors',print_r($errors, true));
                        return $this->render('missing');
                    }
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';
                $this->_helper->layout->disableLayout();
                break;
        }
        
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->crit($this->view->message, $errors->exception);
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $this->view->request   = $errors->request;
        if (!IS_LIVE) {
            $this->view->errors = $errors;
            $this->render('error-dev');
        } else {
            ob_clean();
            echo file_get_contents(PUBLIC_PATH . '/500.html');
            exit;
            file_put_contents('/tmp/errors-'.APPLICATION_ENV, print_r($errors, true));
        }
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasPluginResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}

